import { SyncComponent } from './../dataSync/sync.component';
import { NavController } from 'ionic-angular';
import { Remember } from './../../model/clerkDataContext';
import { MessageHandleManager } from './../../common/messageHandle/msgHandleMg';
import { FNCManager } from './../../common/fncManager';
import { MdSnackBar } from '@angular/material';
import { NavigateService } from './../../service/navigate.service';
import { Component, OnInit, HostListener } from '@angular/core';
import { Authentication } from "../../service/index";
import { MessageBoxService, GrowlMessageType } from "../../component/index";
import { loadingMask } from './../../component/decorators';
import { MD5 } from '../../common/md5/md5';

declare let EventRegistHandle: any;
@Component({
    //moduleId: module.id,
    selector: 'employee-login',
    templateUrl: './employeeLogin.component.html',
    styleUrls: ['./employeeLogin.component.scss']
})
export class EmployeeLoginComponent implements OnInit {
    title: string = "店员登录";
    username = "";
    password = "";
    store = <any>{};
    checked = false;
    showUserList = false;
    isRemember: boolean;
    rememberList: Remember[] = []; //记住密码数组
    lastUserName: string = "";
    constructor(
        private authSvr: Authentication,
        private router: NavigateService,
        private msgSvr: MessageBoxService,
        public snackBar: MdSnackBar,
        public navCtrl: NavController
    ) { }

    @HostListener('document:keyup', ['$event'])
    onKeyUp(ev: KeyboardEvent) {
        if (ev.keyCode == 13) {
            this.login();
        }
    }
    ngOnInit() {
        this.checkUpdate();
        EventRegistHandle.Call("IsOnlineHeart");
        if (this.authSvr.CurrentStore) {
            this.store = this.authSvr.CurrentStore;
        }
        // 获取记住的密码数组
        this.getRemember();
        if (this.authSvr.CurrentUser) {
            let name = this.authSvr.CurrentUser.name;
            if (name && name.length < 2) {
                this.lastUserName = name;
            } else {
                this.lastUserName = name.substring(name.length - 2, name.length)
            }
        }
    }
    onClickMe() {
    }
    @loadingMask()
    async login() {
        try {
            await this.authSvr.employeeLogin(this.username, this.password, this.checked);
            // this.router.navigate(['dataSync']);
            this.navCtrl.setRoot(SyncComponent);
        }
        catch (err) {
            this.msgSvr.Growl(GrowlMessageType.Error, this.snackBar, err ? err : "登录失败");
        }
    }

    // 获取记住密码数组
    private getRemember() {
        let list = localStorage.getItem("rememberList");
        if (list) {
            this.rememberList = JSON.parse(list);
            let empty = this.rememberList.find((item) => {
                return item.username == "";
            })
            if (empty) {
                this.rememberList.splice(this.rememberList.indexOf(empty), 1);
            } else {
                if (this.rememberList.length > 0) {
                    this.username = this.rememberList[this.rememberList.length - 1].username;
                    this.password = this.rememberList[this.rememberList.length - 1].password;
                    this.checked = this.rememberList[this.rememberList.length - 1].isRemember;
                }
            }

        }
    }

    toggleUserList() {
        this.showUserList = !this.showUserList;
    }

    toggleUser(evt) {
        this.username = evt.username;
        this.password = evt.password;
        this.checked = evt.isRemember;
        if (this.showUserList) {
            this.showUserList = !this.showUserList;
        }
    }

    closeUser() {
        this.username = "";
        this.password = "";
        this.checked = false;
    }

    closePassword() {
        this.password = "";
    }

    removeItem(evt) {
        this.rememberList.splice(evt, 1);
        localStorage.setItem('rememberList', JSON.stringify(this.rememberList));
    }

    pressTarget(evt) {
        if (evt.keyCode == 8) {
            this.password = "";
        }
    }

    /**
     * 判断是否下次更新
     */
    checkUpdate() {
        // let isUpdateNextTime = localStorage.getItem("updateNextTime");
        // if (isUpdateNextTime) {
        //     localStorage.removeItem("updateNextTime");
        // } else {
        (<any>window).appUpdate.update();
        // }
    }
}
